﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Class_DTO;
using System.Data;
using System.Data.SqlClient;

namespace Class_DAO
{
    public class ThongTinQCDAO
    {
        public DataTable getDanhSachQC()
        {
            DataProvider.GetConnection();
            SqlConnection cn = DataProvider.GetConnection();
            cn.Open();
            SqlDataAdapter ad = new SqlDataAdapter("getQuangCao", cn);
            DataTable dt = new DataTable();
            ad.Fill(dt);
            cn.Close();
            return dt;

        }

        public DataTable getQuangCao_theoma(int pMa)
        {
            DataProvider.GetConnection();
            SqlConnection cn = DataProvider.GetConnection();
            cn.Open();
            SqlDataAdapter ad = new SqlDataAdapter("[getQuangCao_theoma]", cn);
            SqlCommand cmd = new SqlCommand("getQuangCao_theoma", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@maqc", SqlDbType.Int);
            cmd.Parameters["@maqc"].Value = pMa;
            ad.SelectCommand = cmd;
            DataTable dt = new DataTable();
            ad.Fill(dt);
            cn.Close();
            return dt;
        }

        public void ThemQuangCao_DAO(ThongTinQCDTO qcDTO)
        {
            DataProvider.GetConnection();
            SqlConnection cn = DataProvider.GetConnection();
            cn.Open();
            SqlCommand cmd = new SqlCommand("insertQC", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@maqc", SqlDbType.Int);
            cmd.Parameters.Add("@tenqc", SqlDbType.NVarChar);
           // cmd.Parameters.Add("@chieudai", SqlDbType.Int );
            cmd.Parameters.Add("@kichthuoc", SqlDbType.Int   );
            cmd.Parameters.Add("@tungay", SqlDbType.DateTime );
            cmd.Parameters.Add("@denngay", SqlDbType.DateTime );
            cmd.Parameters.Add("@trangdang", SqlDbType.Int);
            cmd.Parameters.Add("@solandang", SqlDbType.Int);
            cmd.Parameters.Add("@makh", SqlDbType.Int);
            cmd.Parameters.Add("@malqc", SqlDbType.Int);
            cmd.Parameters.Add("@noidung", SqlDbType.NVarChar);
            cmd.Parameters["@maqc"].Direction = ParameterDirection.Output;

            cmd.Parameters["@tenqc"].Value = qcDTO.Tenqc ;
            //cmd.Parameters["@chieudai"].Value = qcDTO.Chieudai;
            cmd.Parameters["@kichthuoc"].Value = qcDTO.Kichthuoc;
            cmd.Parameters["@tungay"].Value = qcDTO.Tungay ;
            cmd.Parameters["@denngay"].Value = qcDTO.Denngay;
            cmd.Parameters["@trangdang"].Value = qcDTO.Trangdang;
            cmd.Parameters["@solandang"].Value = qcDTO.Solandang;
            cmd.Parameters["@makh"].Value = qcDTO.Makh;
            cmd.Parameters["@malqc"].Value = qcDTO.Maloai;
            cmd.Parameters["@noidung"].Value = qcDTO.Noidung;
            cmd.ExecuteNonQuery();

            int mqc = Convert.ToInt32(cmd.Parameters["@maqc"].Value);
            qcDTO.Maqc = mqc;
            cn.Close();
        }
        // sua lam theo mo hinh khac
        public void sua(int ma, string ten,  int kichthuoc, DateTime tungay, DateTime denngay, int trangdang, int solandang, int makh, int mlqc, string noidung)
        {
            SqlParameter[] sp = new SqlParameter[10];
            sp[0] = new SqlParameter("@maqc", ma);
            sp[1] = new SqlParameter("@tenqc", ten);
           // sp[2] = new SqlParameter("@chieudai", chieudai);
            sp[2] = new SqlParameter("@kichthuoc", kichthuoc);
            sp[3] = new SqlParameter("@tungay", tungay);
            sp[4] = new SqlParameter("@denngay", denngay);
            sp[5] = new SqlParameter("@trangdang", trangdang);
            sp[6] = new SqlParameter("@solandang", solandang);
            sp[7] = new SqlParameter("@makh", makh);
            sp[8] = new SqlParameter("@malqc", mlqc);
            sp[0] = new SqlParameter("@noidung", noidung);
            DataProvider.proc("updateQC", sp);
        }
        public void xoa(int s1)
        {
            SqlParameter[] sp = new SqlParameter[1];
            sp[0] = new SqlParameter("@maqc", s1);
            DataProvider.proc("deleteQuangCao", sp);
        }
        
    }
}
